package com.alm.token.ui.activity;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.utils.ActionBarHelper;
import com.alm.token.utils.LogUtil;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.greenrobot.eventbus.EventBus;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import baibeiwallet.event.RefreshMeEvent;
import baibeiwallet.utils.MnemonicUtils;
import butterknife.BindView;
import butterknife.OnClick;

public class ExportMnemonicActivity extends BaseActivity {

    @BindView(R.id.id_flowlayout_sel)
    TagFlowLayout mFlowLayoutSel;
    @BindView(R.id.id_flowlayout)
    TagFlowLayout mFlowLayout;
    @BindView(R.id.id_flowlayout_remember)
    TagFlowLayout mFlowLayoutRemember;
    @BindView(R.id.id_flowlayout_rememberbody)
    LinearLayout mFlowLayoutRememberBody;
    @BindView(R.id.tv)
    TextView mTv;
    @BindView(R.id.tv_check)
    TextView mTvCheck;
    @BindView(R.id.tv_scanonce)
    TextView mTvScanonce;
    @BindView(R.id.tv_flowlayout_title)
    TextView mFlowLayoutTvtitle;

    List<MnemonicModel> mList = new ArrayList<>();
    List<MnemonicModel> mListSel = new ArrayList<>();
    List<MnemonicModel> tempList = new ArrayList<>();
    TagAdapter mAdapterSel;
    TagAdapter mAdapter, mAdapterRember;
    public static final int INTENT_Mnemonic_INTO_CODE = 1001;//
    public static final int INTENT_Mnemonic_RESULT_SUCC_CODE = 1002;//请求码正确
    public static final String  INTENT_Mnemonic = "INTENT_Mnemonic";//请求码正确
    String result;

    @Override
    protected MyPreenter initPreenter() {
        return null;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_export_mnemonic4;
    }

    @Override
    protected void initVariables() {
        result = getIntent().getStringExtra("ExportMnemonic");
    }

    @Override
    protected void initViews() {
        new ActionBarHelper(this).setTitle(getString(R.string.register_hint_monic_title));
        mTvCheck.setText(getString(R.string.register_hint_monic_scan));
        mFlowLayoutRemember.setVisibility(View.VISIBLE);
        mFlowLayoutRememberBody.setVisibility(View.VISIBLE);
        mFlowLayoutSel.setVisibility(View.GONE);
        mFlowLayout.setVisibility(View.GONE);
        mFlowLayoutTvtitle.setVisibility(View.GONE);
        mTvScanonce.setVisibility(View.GONE);
        mTvCheck.setText(getString(R.string.register_hint_monic_scan));
    }

    @Override
    protected void loadData() {
        initData();
        initFlowLayoutSel();
        init();
    }

    private void initData() {
        LogUtil.i("mnemonic: 获取的助记符 = " + result);

        String[] mnemonics = result.split(" ");
        //tempList0  最初的显示  需要用户记住
        //mList   用户需要校验
        List<MnemonicModel> tempList0 = new ArrayList<>();
        for (int i = 0; i < mnemonics.length; i++) {
            tempList0.add(new MnemonicModel(mnemonics[i], i));
            tempList.add(new MnemonicModel(mnemonics[i], i));
//            mList.add(new MnemonicModel(mnemonics[i], i));
        }

//        Collections.sort(mList, new Comparator<MnemonicModel>() {
//            @Override
//            public int compare(MnemonicModel o1, MnemonicModel o2) {
//                return compare(o1, o2);
//            }
//        });

        int size = tempList0.size();
        for (int i = 0; i < size; i++) {
            int sel = (int) (Math.random() * 100 % tempList0.size());
            mList.add(tempList0.remove(sel));
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mList.size(); i++) {
            sb.append(mList.get(i).mnemonic + " ");
        }
        mTv.setText(result + "\n" + sb.toString());
    }

    private void init() {

        mAdapterRember = new TagAdapter<MnemonicModel>(tempList) {
            @Override
            public View getView(FlowLayout parent, int position, MnemonicModel s) {
                View view = LayoutInflater.from(ExportMnemonicActivity.this).inflate(R.layout.item_tag_mnemonic_rember,
                        mFlowLayoutRemember, false);
                TextView tv = view.findViewById(R.id.tv);
                s.index = position;
                tv.setText(position+1+". "+s.mnemonic);
                return view;
            }
        };

        mFlowLayoutRemember.setAdapter(mAdapterRember);
//        mFlowLayout.setOnTagClickListener((view, position, parent) -> {
//            boolean has = mListSel.contains(mList.get(position));
//            if (has) {
//                mListSel.remove(mList.get(position));
//            } else {
//                mListSel.add(mList.get(position));
//            }
//            mAdapterSel.notifyDataChanged();
//            return false;
//        });


        mAdapter = new TagAdapter<MnemonicModel>(mList) {
            @Override
            public View getView(FlowLayout parent, int position, MnemonicModel s) {
                View view = LayoutInflater.from(ExportMnemonicActivity.this).inflate(R.layout.item_tag_mnemonic_sel3,
                        mFlowLayout, false);
                TextView tv = view.findViewById(R.id.tv);
                s.index = position;
                tv.setText(s.mnemonic);
                return view;
            }
        };

        mFlowLayout.setAdapter(mAdapter);
        mFlowLayout.setOnTagClickListener((view, position, parent) -> {
            boolean has = mListSel.contains(mList.get(position));
            if (has) {
                mListSel.remove(mList.get(position));
            } else {
                mListSel.add(mList.get(position));
            }
            mAdapterSel.notifyDataChanged();
            return false;
        });
    }

    private void initFlowLayoutSel() {
        if (mAdapterSel == null) {
            mAdapterSel = new TagAdapter<MnemonicModel>(mListSel) {
                @Override
                public View getView(FlowLayout parent, int position, MnemonicModel s) {
                    View view = LayoutInflater.from(ExportMnemonicActivity.this).inflate(R.layout.item_tag_mnemonic3,
                            mFlowLayout, false);
                    TextView tv = view.findViewById(R.id.tv);
                    tv.setText(s.mnemonic);
                    return view;
                }
            };
        }
        mFlowLayoutSel.setAdapter(mAdapterSel);
//        mFlowLayoutSel.setOnTagClickListener((view, position, parent) -> {
//            MnemonicModel model = mListSel.remove(position);
//            Set<Integer> sel = mFlowLayout.getSelectedList();
//            sel.remove(model.index);
//            mAdapter.setSelectedList(sel);
//            mAdapterSel.notifyDataChanged();
//            return false;
//        });
    }

    @OnClick(R.id.tv)
    public void onClick() {
//        initData();
    }

    @OnClick({R.id.tv_check,R.id.tv_scanonce})
    public void onClickCheck(View view) {

        switch (view.getId()){

            case  R.id.tv_scanonce:

                //再次查看
                mFlowLayoutRemember.setVisibility(View.VISIBLE);
                mFlowLayoutRememberBody.setVisibility(View.VISIBLE);
                mFlowLayoutSel.setVisibility(View.GONE);
                mFlowLayout.setVisibility(View.GONE);
                mFlowLayoutTvtitle.setVisibility(View.GONE);
                mTvScanonce.setVisibility(View.GONE);
                mTvCheck.setText(getString(R.string.register_hint_monic_scan));
                break;
            case  R.id.tv_check:

                if (mTvCheck.getText().toString().equalsIgnoreCase(getString(R.string.register_hint_monic_scan))) {

                    //开始校验
                    mFlowLayoutRemember.setVisibility(View.GONE);
                    mFlowLayoutRememberBody.setVisibility(View.GONE);
                    mFlowLayoutSel.setVisibility(View.VISIBLE);
                    mFlowLayout.setVisibility(View.VISIBLE);
                    mFlowLayoutTvtitle.setVisibility(View.VISIBLE);
                    mTvScanonce.setVisibility(View.VISIBLE);
                    mTvCheck.setText(getString(R.string.register_hint_monic_check));

                } else if (mTvCheck.getText().toString().equalsIgnoreCase(getString(R.string.register_hint_monic_check))) {

                    //开始校验
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < mListSel.size(); i++) {
                        sb.append(mListSel.get(i).mnemonic + (i != mListSel.size() - 1 ? " " : ""));
                    }
                    LogUtil.e("校验" + result + "\n" + sb.toString());
                    if (result.equalsIgnoreCase(sb.toString())) {
                        showToast(getString(R.string.register_hint_monic_check_succ));
                        Intent intent = new Intent();
                        intent.putExtra(INTENT_Mnemonic, result);
                        setResult(INTENT_Mnemonic_RESULT_SUCC_CODE, intent);
                        EventBus.getDefault().post(new RefreshMeEvent(1));
                        finish();
                    } else {
                        showToast(getString(R.string.register_hint_monic_check_err));
                    }
                }
                break;
        }


    }

    @Override
    public void onClick(View v) {

    }

    class MnemonicModel {
        String mnemonic;
        int index;

        public MnemonicModel(String mnemonic, int index) {
            this.mnemonic = mnemonic;
            this.index = index;
        }


    }
}
